package com.owen.shiro;

import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;

/**
 * 自定义Realm
 */
public class UserRealm extends AuthorizingRealm {


    // 执行授权逻辑
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        System.out.println("执行授权逻辑");
        return null;
    }


    // 执行认证逻辑
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        // 即登录逻辑， 判断用户名和密码
        // 假设正确的用户名和密码如下
        String name = "admin";
        String password = "123456";

        // 获取用户输入的用户名和看密码
        UsernamePasswordToken token = (UsernamePasswordToken)authenticationToken;
        // 判断用户名是否存在（用户名是否正确）
        if(!token.getUsername().equals(name)){
            // 用户名不存在
            return null;// Shiro底层会抛出UnknownAccountException异常
        }

        // 判断密码 password是数据库中正确的密码
        return new SimpleAuthenticationInfo("",password,"");
    }
}